/*
 * File:   Node.h
 * Author: henrya
 *
 * Created on June 16, 2009, 3:05 PM
 */

#ifndef _NODE_H
#define _NODE_H

#include <vector>
#include <string>
#include "OSThread.h"
#include "Message.h"
#include "SynchronizedPriorityQueue.h"
using std::string;
using std::vector;

class Network;
class Mailer;
class RoutingTable;
class SynchronizedPriorityQueue;
class Shell;


class Node : public OSThread {
public:// methods
    Node(Shell* shell,Mailer* mailer, Network* network, int id);
    virtual ~Node();
    void addNeighbour(int id);
    const int getId();
    string* printRoutingTable();
    static Node* createNode(Shell* shell, Mailer* mailer, Network* network, string& nodeConf);
    void addMessage(Message* msg);
    unsigned int getInboxSize();
private:
    vector<int> neighboursIds;
    Network* network;
    const int id;
    volatile bool interrupted;
    SynchronizedPriorityQueue inbox;
    RoutingTable* routingTable;
    Mailer* mailer;
    vector<int> killedIds;
    Shell* shell;

    void sendToNeigbour(int neigbourId,Message* msg);
    void sendRTToAllNeigbours();
    void resetRT();
    void setNextHop(int finalDest, Message* msg);
public:
    virtual void run();
    virtual bool studentSend(void *args);
    virtual bool studentRecv(void **args);
};

#endif	/* _NODE_H */
